added samples
[windows-sources.git] / sdk / samples / all in on code / Visual Studio 2008 / CppMailslotClient / CppMailslotClient.cpp
blob077cc0e6adcf09076ac88aadb424c267134a3e64
1 /****************************** Module Header ******************************\
2 Module Name: CppMailslotClient.cpp
3 Project: CppMailslotClient
4 Copyright (c) Microsoft Corporation.
6 Mailslot is a mechanism for one-way inter-process communication in the local
7 machine or across the computers in the intranet. Any clients can store
8 messages in a mailslot. The creator of the slot, i.e. the server, retrieves
9 the messages that are stored there:
11 Client (GENERIC_WRITE) ---> Server (GENERIC_READ)
13 This sample demonstrates a mailslot client that connects and writes to the
14 mailslot "\\.\mailslot\SampleMailslot".
16 This source is subject to the Microsoft Public License.
17 See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
18 All other rights reserved.
20 THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
21 EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
22 WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
23 \***************************************************************************/
25 #pragma region Includes
26 #include <stdio.h>
27 #include <windows.h>
28 #include <assert.h>
29 #pragma endregion
32 void WriteMailslot(HANDLE hMailslot, PCWSTR pszMessage);
35 int wmain(int argc, wchar_t *argv[])
37 // The name of the mailslot. It is in the form of \\.\mailslot\[path]name
38 // The name field must be unique. The name may include multiple levels of
39 // pseudo directories separated by backslashes. For example, both
40 // \\.\mailslot\mailslot_name and \\.\mailslot\abc\def\ghi are valid.
41 const PCWSTR MAILSLOT_NAME = L"\\\\.\\mailslot\\SampleMailslot";
43 HANDLE hMailslot = INVALID_HANDLE_VALUE;
44 DWORD dwError = ERROR_SUCCESS;
46 // Open the mailslot with write access (Mailslot is a mechanism for one-
47 // way IPC. The client is just responsible for writing to the mailslot.)
48 hMailslot = CreateFile(
49 MAILSLOT_NAME, // The name of the mailslot
50 GENERIC_WRITE, // Write access
51 FILE_SHARE_READ, // Share mode
52 NULL, // Default security attributes
53 OPEN_EXISTING, // Opens existing mailslot
54 FILE_ATTRIBUTE_NORMAL, // The file has no other attributes set
55 NULL // No template file
57 if (hMailslot == INVALID_HANDLE_VALUE)
59 dwError = GetLastError();
60 wprintf(L"Unable to open mailslot w/err 0x%08lx\n", dwError);
61 goto Cleanup;
64 wprintf(L"The mailslot (%s) is opened.\n", MAILSLOT_NAME);
66 // Write messages to the mailslot.
68 WriteMailslot(hMailslot, L"Message 1 for mailslot");
69 WriteMailslot(hMailslot, L"Message 2 for mailslot");
70 Sleep(3000); // Sleep for 3 seconds for the demo purpose
71 WriteMailslot(hMailslot, L"Message 3 for mailslot");
73 Cleanup:
75 // Centralized cleanup for all allocated resources.
76 if (hMailslot != INVALID_HANDLE_VALUE)
78 CloseHandle(hMailslot);
79 hMailslot = INVALID_HANDLE_VALUE;
82 return dwError;
87 // FUNCTION: WriteMailslot(HANDLE, PCWSTR);
89 // PURPOSE: Write a message to the specified mailslot.
91 // PARAMETERS:
92 // * hMailslot - The handle of the mailslot.
93 // * pszMessage - The message to be written to the slot.
95 void WriteMailslot(HANDLE hMailslot, PCWSTR pszMessage)
97 assert(hMailslot != INVALID_HANDLE_VALUE);
98 assert(pszMessage != NULL);
100 DWORD cbMessageBytes = 0; // Message size in bytes
101 DWORD cbBytesWritten = 0; // Number of bytes written to the slot
103 // Calculate the message size (including the NULL terminator) in bytes.
104 cbMessageBytes = (wcslen(pszMessage) + 1) * sizeof(*pszMessage);
106 BOOL fSucceeded = WriteFile( // Write to the mailslot.
107 hMailslot, // Handle of the slot
108 pszMessage, // Message to be written
109 cbMessageBytes, // Number of bytes to write
110 &cbBytesWritten, // Number of bytes written
111 NULL // Not overlapped
114 if (!fSucceeded || cbMessageBytes != cbBytesWritten)
116 wprintf(L"WriteFile failed w/err 0x%08lx\n", GetLastError());
118 else
120 wprintf(L"The message \"%s\" is written to the slot\n", pszMessage);